<?php

namespace catchAdmin\model;

use PosterMaker\PosterMaker;

class WechatUser extends ModelApp
{

    protected $append = [
        'avatar_show',
        'company_status_name',
        'company_type_name',
        'last_login_time_show',
        'share_img_show',
    ];

    protected $hidden = [
        'mini_openid',
        'openid',
    ];


    ################################### 关系 start ###########################

    public function userCompany()
    {
        return $this->hasOne(UserCompany::class, 'wechat_user_id', 'id');
    }

    ################################### 关系 end ###########################
    ################################### Attr start ###########################

    /**
     * avatar_show
     */
    public function getAvatarShowAttr()
    {
        if (empty($this->getOrigin('avatar'))) {
            return request()->domain() . '/img/phone/mini/header.png';
        }

        return request()->domain() . $this->getOrigin('avatar');
    }


    /**
     * need_count
     */
    public function getNeedCountAttr()
    {
        return (new Need())
            ->where('wechat_user_id', $this->getOrigin('id'))
            ->count();
    }


    /**
     * share_code
     */
    public function getShareCodeAttr()
    {
        if ($this->getOrigin('company_status') != 3) {
            return "";
        }

        return $this->getOrigin('share_code');
    }


    /**
     * share_img
     */
    public function getShareImgAttr()
    {
        if ($this->getOrigin('company_status') != 3) {
            return "";
        }

        return $this->getOrigin('share_img');
    }


    /**
     * share_img_show
     */
    public function getShareImgShowAttr()
    {
        if ($this->getOrigin('company_status') != 3) {
            return "";
        }

        return request()->domain() . $this->getOrigin('share_img');
    }


    /**
     * 累计收益
     * all_income
     */
    public function getAllIncomeAttr()
    {
        return mathNumPoint((new WechatUserBalance())
            ->where('wechat_user_id', $this->getLoginId())
            ->where('balance_type', 3)
            ->where('balance_type_2', 301)
            ->sum('num'));
    }


    /**
     * 推广订单
     * all_share_order
     */
    public function getAllShareOrderAttr()
    {
        return 1;
    }


    /**
     * 团队人数
     * all_user_num
     */
    public function getAllUserNumAttr()
    {
        return (new WechatUser())
            ->where('parent_id', $this->getLoginId())
            ->count();
    }


    /**
     * 累计提现
     * all_withdraw
     */
    public function getAllWithdrawAttr()
    {
        return mathNumPoint((new WechatUserBalance())
            ->where('wechat_user_id', $this->getLoginId())
            ->where('balance_type', 3)
            ->where('balance_type_2', 302)
            ->sum('num'));
    }


    /**
     * 即将到账
     * coming_income
     */
    public function getComingIncomeAttr()
    {
        return mathNumPoint(0);
    }


    /**
     * 未读消息数
     * notice_not_read_count
     */
    public function getNoticeNotReadCountAttr()
    {
        $a = \catchAdmin\model\Notice::where('wechat_user_id', $this->getOrigin('id'))
            ->where('read_time', 0)
            ->count() ?? 0;

        $b = (new SiXin())
            ->where('to_user_id', $this->getOrigin('id'))
            ->where('read_time', 0)
            ->count() ?? 0;

        return $a + $b;
    }


    /**
     * last_login_time_show
     */
    public function getLastLoginTimeShowAttr()
    {
        return date('Y-m-d H:i:s', $this->getOrigin('last_login_time'));
    }


    /**
     * company_type_name_list
     */
    public function getCompanyTypeNameList()
    {
        return [
            0 => '未注册',
            1 => '居间人',
            2 => '工程公司',
            3 => '物资工厂',
            4 => '设计公司',
            5 => '监理公司',
            6 => '劳务公司',
            7 => '物资贸易商',
            8 => '协会',
        ];
    }


    /**
     * company_type_name
     */
    public function getCompanyTypeNameAttr()
    {
        return $this->getCompanyTypeNameList()[$this->getOrigin('company_type')] ?? '';
    }


    /**
     * company_status_name_list
     */
    public function getCompanyStatusNameList()
    {
        return [
            1 => '未提交',
            2 => '审核中',
            3 => '审核通过',
            4 => '审核未通过',
        ];
    }


    /**
     * company_status_name
     */
    public function getCompanyStatusNameAttr()
    {
        return $this->getCompanyStatusNameList()[$this->getOrigin('company_status')] ?? '';
    }


    ################################### Attr end ###########################
    ############################ search start ###############################

    public function searchCompanyTypeAttr($query, $value, $data)
    {
        $query->where('company_type', $value);
    }


    public function searchCompanyStatusAttr($query, $value, $data)
    {
        $query->where('company_status', $value);
    }


    public function searchCompanyNameAttr($query, $value, $data)
    {
        $query->where('company_name', 'like', '%' . $value . '%');
    }


    public function searchNicknameAttr($query, $value, $data)
    {
        $query->where('nickname', 'like', '%' . $value . '%');
    }


    public function searchNameAttr($query, $value, $data)
    {
        $query->where('name', 'like', '%' . $value . '%');
    }


    public function searchPhoneAttr($query, $value, $data)
    {
        $query->where('phone', 'like', '%' . $value . '%');
    }


    public function searchShareCodeAttr($query, $value, $data)
    {
        $query->where('share_code', 'like', '%' . $value . '%');
    }

    ############################ search end ###############################
    ################################### func start ###########################

    /**
     * 生成分享码
     */
    public function getShareCode()
    {
        $share_code = "";
        while (1) {
            $characters = '0123456789ABCDEFGHJKLMNOPRSTUVWXYZ';
            for ($i = 0; $i < 5; $i++) {
                $share_code .= $characters[rand(0, strlen($characters) - 1)];
            }

            if (empty($this
                ->where('share_code', $share_code)
                ->find())) {
                break;
            }
        }

        return $share_code;
    }


    /**
     * 生成分享图片
     */
    public function getShareImg($id, $share_code)
    {
        $dir = "img/phone/share";
        if (!is_dir(public_path() . $dir)) {
            mkdir(public_path() . $dir, 0777, true);
        }

        $file_name = $dir . '/' . $id . "_" . rand(1000, 9999) . '.jpg';

        (new PosterMaker(800, 1560))
            ->addImg(public_path() . 'img/phone/mini/share_bg.png', [0, 0], [800, 1560])
            ->addQrCode('https://www.dian18.cn/share_code?share_code=' . $share_code, [110, 360], [600, 600])
            ->addText("我的邀请码：" . $share_code, 28, [160, 990], [0, 0, 0])
            ->addText("推广说明：", 28, [150, 1300], [255, 255, 255])
            ->addText("电18电力物资一站式购销平台，诚邀您的加入", 20, [150, 1350], [255, 255, 255])
            ->addText("电力工资源整合平台，出海联盟", 20, [150, 1400], [255, 255, 255])
            ->addText("长按扫码进行注册", 20, [150, 1450], [255, 255, 255])
            ->render(public_path() . $file_name);

        return '/' . $file_name;
    }


    ################################### func end ###########################

}
